﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NoteBook.Utils;

namespace NoteBook
{
    public partial class frmNotepad : Form
    {
        /// <summary>
        ///  布尔变量b用于判断文件是新建的还是从磁盘打开的，true表示文件是从磁盘打开的，false表示文件是新建的，默认值为false
        /// </summary>
        private bool b = false;

        /// <summary>
        /// 是否已保存
        /// </summary>
        bool isSave = true;

        /// <summary>
        /// 上一次打开的对象
        /// </summary>
        private OpenFileDialog odlgNotepad = new OpenFileDialog();

        /// <summary>
        ///  保存
        /// </summary>
        private SaveFileDialog sdlgNotepad = new SaveFileDialog();

        public frmNotepad()
        {
            InitializeComponent();
        }

        // 新建文档
        private void tsmiNew_Click(object sender, EventArgs e)
        {
            // 当前文件为磁盘打开，且
            if (b == true || rtxtNotepad.Text.Trim() != "")
            {
                if (isSave == false)
                {
                    DialogResult result;
                    result = MessageBox.Show(
                        "文件尚未保存,是否保存?",
                        "保存文件",
                        MessageBoxButtons.YesNoCancel
                    );
                    switch (result)
                    {
                        case DialogResult.OK:
                            // 若文件是从磁盘打开的
                            if (b == true)
                            {
                                // 按文件打开的路径保存文件
                                rtxtNotepad.SaveFile(odlgNotepad.FileName);
                            }
                            // 若文件不是从磁盘打开的
                            else if (sdlgNotepad.ShowDialog() == DialogResult.OK)
                            {
                                rtxtNotepad.SaveFile(sdlgNotepad.FileName);
                            }
                            isSave = true;
                            rtxtNotepad.Text = "";
                            break;
                        case DialogResult.No:
                            b = false;
                            rtxtNotepad.Text = "";
                            break;
                    }
                }
            }
        }

        /// <summary>
        /// 打开操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiOpen_Click(object sender, EventArgs e)
        {
            if (b == true || rtxtNotepad.Text.Trim() != "")
            {
                if (isSave == false)
                {
                    DialogResult result;
                    result = MessageBox.Show(
                        "文件尚未保存,是否保存?",
                        "保存文件",
                        MessageBoxButtons.YesNoCancel
                    );
                    switch (result)
                    {
                        case DialogResult.Yes:
                        {
                            if (b == true)
                            {
                                rtxtNotepad.SaveFile(odlgNotepad.FileName);
                            }
                            else if (sdlgNotepad.ShowDialog() == DialogResult.OK)
                            {
                                rtxtNotepad.SaveFile(sdlgNotepad.FileName);
                            }
                            isSave = true;
                            break;
                        }
                        case DialogResult.No:
                        {
                            b = false;
                            rtxtNotepad.Text = "";
                            break;
                        }
                    }
                }
            }

            odlgNotepad.RestoreDirectory = true;
            // Initialize the OpenFileDialog to look for RTF files.
            //odlgNotepad.DefaultExt = "*.rtf";
            //odlgNotepad.Filter = "RTF Files|*.rtf";

            if ((odlgNotepad.ShowDialog() == DialogResult.OK) && odlgNotepad.FileName != "")
            {
                rtxtNotepad.LoadFile(odlgNotepad.FileName, RichTextBoxStreamType.PlainText);
                b = true;
            }
            isSave = true;
        }

        private void tsmiSave_Click(object sender, EventArgs e)
        {
            // 若文件从磁盘打开并且修改了其内容
            if (b == false && rtxtNotepad.Modified == true)
            {
                rtxtNotepad.SaveFile(odlgNotepad.FileName, RichTextBoxStreamType.PlainText);
                isSave = true;
            }
            else if (
                b == false
                && rtxtNotepad.Text.Trim() != ""
                && sdlgNotepad.ShowDialog() == DialogResult.OK
            )
            {
                string fileName = sdlgNotepad.FileName; //文件名
                rtxtNotepad.SaveFile(fileName);
                isSave = true;
                b = true;
                odlgNotepad.FileName = fileName;
            }
        }

        /// <summary>
        ///  关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiClose_Click(object sender, EventArgs e)
        {
           SaveAndClose();
        }

        private void tsmiUndo_Click(object sender, EventArgs e)
        {
            rtxtNotepad.Undo();
        }

        private void tsmiCut_Click(object sender, EventArgs e)
        {
            rtxtNotepad.Cut();
        }

        private void tsmiCopy_Click(object sender, EventArgs e)
        {
            rtxtNotepad.Copy();
        }

        private void tsmiPaste_Click(object sender, EventArgs e)
        {
            rtxtNotepad.Paste();
        }

        private void tsmiDelete_Click(object sender, EventArgs e)
        {
            rtxtNotepad.Clear();
        }

        private void tsmiFind_Click(object sender, EventArgs e)
        {
            MessageHelper.Information("提示", "功能尚未开发请等待");
            //int index = richTextBox1.Find("search text");
            //if (index != -1)
            //{
            //    richTextBox1.Select(index, "search text".Length); richTextBox1.ScrollToCaret();
            //}
        }

        private void tsmiPrefix_Click(object sender, EventArgs e)
        {
            MessageHelper.Information("提示", "功能尚未开发请等待");
        }

        private void tsmiNext_Click(object sender, EventArgs e)
        {
            MessageHelper.Information("提示", "功能尚未开发请等待");
        }

        private void tsmiReplace_Click(object sender, EventArgs e)
        {
            MessageHelper.Information("提示", "功能尚未开发请等待");
            /**
             * richTextBox1.Select(richTextBox1.Text.IndexOf("要查找的文本"), "要查找的文本".Length); // 选择文本  
             * richTextBox1.ReplaceSelection("替换后的文本"); // 替换选中的文本
             */
        }

        private void tmsiGoto_Click(object sender, EventArgs e)
        {
            MessageHelper.Information("提示", "功能尚未开发请等待");
        }

        /// <summary>
        /// 全选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiSelectAll_Click(object sender, EventArgs e)
        {
            rtxtNotepad.SelectAll();
        }

        private void tsmiFont_Click(object sender, EventArgs e) { }

        private void tsmiAbout_Click(object sender, EventArgs e)
        {
            FrmAbout frmAbout = new FrmAbout();
            frmAbout.ShowDialog();
        }

        // 换行开启和关闭
        private void tsmiAuto_Click(object sender, EventArgs e)
        {
            if (tsmiAuto.Checked)
            {
                tsmiAuto.Checked = false;
                rtxtNotepad.WordWrap = false;
            }
            else
            {
                tsmiAuto.Checked = true;
                rtxtNotepad.WordWrap = true;
            }
        }

        /// <summary>
        /// 实时获取光标位置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rtxtNotepad_SelectionChanged(object sender, EventArgs e)
        {
            //当前光标所在位置
            //当前行的索引
            int index = this.rtxtNotepad.GetFirstCharIndexOfCurrentLine();
            //共有行数
            int intLines = this.rtxtNotepad.GetLineFromCharIndex(this.rtxtNotepad.TextLength) + 1;
            //得到光标的行号
            int line = this.rtxtNotepad.GetLineFromCharIndex(index) + 1;
            //得到光标列的索引
            /*SelectionStart得到光标所在位置的索引
                再减去
                当前行第一个字符的索引
                = 光标所在的列数(从0开始）
            */
            int column = this.rtxtNotepad.SelectionStart - index + 1;
            //this.richTxt.Paste(Clipboard.GetDataObject());
            Console.WriteLine($"cursorPosition==>第{line}行{column}列,共{intLines}行");
            //this.toolStripStatusLabel_Row.Text = string.Format("第{0}行{1}列,共{2}行", line.ToString(), column.ToString(), intLines.ToString());
            tssLocation.Text = string.Format(
                "第{0}行，第{1}列",
                line.ToString(),
                column.ToString()
            );
        }

        private void rtxtNotepad_TextChanged(object sender, EventArgs e)
        {
            /// 保存操作
            isSave = false;
        }

        private void tsmiSaveAs_Click(object sender, EventArgs e)
        {
            if (sdlgNotepad.ShowDialog() == DialogResult.OK)
            {
                rtxtNotepad.SaveFile(sdlgNotepad.FileName);
                isSave = true;
            }
        }

        private void tsmiHelpDoc_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.baidu.com/"); //打开百度网页
        }

        private void frmNotepad_FormClosed(object sender, FormClosedEventArgs e)
        {
            SaveAndClose();
        }

        // 关闭前保存文件
        private void SaveAndClose()
        {
            if (b == true || rtxtNotepad.Text.Trim() != "")
            {
                if (isSave == false)
                {
                    DialogResult result;
                    result = MessageBox.Show(
                        "文件尚未保存,是否保存?",
                        "保存文件",
                        MessageBoxButtons.YesNoCancel
                    );
                    switch (result)
                    {
                        case DialogResult.Yes:
                            {
                                if (b == true)
                                {
                                    rtxtNotepad.SaveFile(odlgNotepad.FileName);
                                }
                                else if (sdlgNotepad.ShowDialog() == DialogResult.OK)
                                {
                                    rtxtNotepad.SaveFile(sdlgNotepad.FileName);
                                }
                                isSave = true;
                                Application.Exit();
                                break;
                            }
                        case DialogResult.No:
                            {
                                b = false;
                                rtxtNotepad.Text = "";
                                Application.Exit();
                                break;
                            }
                    }
                }
            }
        }

        private void toolStripStatusLabel4_Click(object sender, EventArgs e)
        {

        }
    }
}
